<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../../css/header.css" />
	</head>

	<body>

		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: white;"></a>
			<a id="link_submit_add_user" class="mui-icon mui-icon-right-nav mui-pull-right" 
							style="color: white;font-size: 14px;line-height: 27px;">提交</a>
			<h1 class="mui-title title-color">添加新用户</h1>
		</header>

		<div class="mui-content">
			<div class="mui-card">
				<div class="mui-input-row">
					<label>用户账号</label>
					<input id="link_user_id" type="text" class="mui-input-clear" placeholder="请输入用户账号">
				</div>
				<div class="mui-input-row">
					<label>用户昵称</label>
					<input id="link_user_name" type="text" class="mui-input-clear" placeholder="请输入用户昵称">
				</div>
				<div class="mui-input-row">
					<label>班级id</label>
					<select id="link_class_id">
						<!-- <option value="2">教师</option> -->
					</select>
				</div>
				<div class="mui-input-row">
					<label>角色类型</label>
					<select id="link_user_type">
						<option value="2">教师</option>
						<option value="3">学生</option>
					</select>
				</div>
			</div>
		
		</div>

		<script src="../../js/mui.min.js"></script>
		<script src="../../js/app.js"></script>
		<script type="text/javascript">
			mui.init()

			mui.plusReady(function() {
				
				// 获取 class_id
				mui.ajax(app.serverUrl + "/user/admin/query_class_info", {
					data: {},
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					headers: {
						'Content-Type': 'application/json'
					},
					success: function(data) {
						if (data.status == 200) {
							var classInfoList = data.data;
							
							var link_class_id = document.getElementById("link_class_id");
							
							if (classInfoList != null && classInfoList.length > 0) {
								var class_id_html_list = "";
								for (var i = 0; i < classInfoList.length; i++) {
									class_id_html_list += classInfoHtml(classInfoList[i]);
								}
								link_class_id.innerHTML = class_id_html_list;
							} else {
								link_class_id.innerHTML = "";
							}
						} else {
							app.showToast("获取数据失败，请重试！", "error");
						}
					}
				});
				
				var link_submit_add_user = document.getElementById("link_submit_add_user");
				
				var link_user_id = document.getElementById("link_user_id");
				
				var link_user_name = document.getElementById("link_user_name");
				
				var link_user_type = document.getElementById("link_user_type");
				
				link_submit_add_user.addEventListener("tap",function(){
					
					mui.ajax(app.serverUrl + "/user/admin/insert_userinfo?user_id=" + link_user_id.value
								+ "&name=" + link_user_name.value
								+ "&class_id=" + link_class_id.value
								+ "&role_type=" + link_user_type.value, {
						data: {},
						dataType: 'json', //服务器返回json格式数据
						type: 'post', //HTTP请求类型
						timeout: 10000, //超时时间设置为10秒；
						headers: {
							'Content-Type': 'application/json'
						},
						success: function(data) {
							if (data.status == 200) {
								app.showToast("成功添加用户！", "success");
								// 打开用户功能列表页面
								mui.openWindow({
									url: "manager.html",
									id: "manager.html"
								});
							} else {
								app.showToast("获取数据失败，请重试！", "error");
							}
						}
					});
					
					
				});
				
			});
			
			function classInfoHtml(class_info) {
				var html = "";
				html = '<option value="' + class_info.class_id + '">' + class_info.class_name + '</option>';
				return html;
			};
			
			// function get_class_info() {
			// 	mui.ajax(app.serverUrl + "/user/admin/query_class_info", {
			// 		data: {},
			// 		dataType: 'json', //服务器返回json格式数据
			// 		type: 'post', //HTTP请求类型
			// 		timeout: 10000, //超时时间设置为10秒；
			// 		headers: {
			// 			'Content-Type': 'application/json'
			// 		},
			// 		success: function(data) {
			// 			if (data.status == 200) {
			// 				return data.data;
			// 			} else {
			// 				app.showToast("获取数据失败，请重试！", "error");
			// 			}
			// 		}
			// 	});
			// }
			
		</script>
	</body>

</html>